package com.qububu.play.dineTogether.service.impl;

import java.util.List;

import javax.annotation.Resource;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

import org.springframework.stereotype.Service;

import com.qububu.db.dao.impl.DineSummarizeDaoImpl;
import com.qububu.play.dineTogether.dto.DineSummarizeDto;
import com.qububu.play.dineTogether.service.DineSummarizeService;
import com.qububu.utils.ParamConverter;
import com.qububu.utils.SystemUtil;

@Service
public class DineSummarizeServiceImpl implements DineSummarizeService {

	@Resource
	private DineSummarizeDaoImpl dineSummarizeDao;
	
	@Resource
	private Cache dineSummarizeCache;
	private final int dineSummarizeListKey = -1;
	
	public boolean doAddSummarize(DineSummarizeDto dineSummarize) {
	
		int key = ParamConverter.convertToInteger(SystemUtil.decrypt(dineSummarize.getActionId()));
		if (removeElementFromCache(key)) {
			dineSummarizeDao.addSummarize(dineSummarize);
			return true;
		}
		return false;
	}

	/**
	 * 给出最新几次活动的总结
	 * */
	public List<DineSummarizeDto> getDineSummarizes() {
		List<DineSummarizeDto> summarizeList = (List<DineSummarizeDto>) getElementFromCache(dineSummarizeListKey);
		if (summarizeList == null) {
			summarizeList = dineSummarizeDao.getDineSummarizes();
			doSaveElemToCache(dineSummarizeListKey, summarizeList);
		}
		return summarizeList;
	}
	
	/**
	 * 暂时不用
	 * */
	public DineSummarizeDto getSummarizeByActionId(String actionId) {
		return dineSummarizeDao.getSummarizeByActionId(actionId);
	}

	public boolean doModifyDineSummarize(DineSummarizeDto dineSummarize) {
		int key = ParamConverter.convertToInteger(SystemUtil.decrypt(dineSummarize.getActionId()));
		if (removeElementFromCache(key)) {
			dineSummarizeDao.modifySummarize(dineSummarize);
			
			return true;
		}
		return false;
	}

	public boolean doDeleteSummarize(String summarizeId) {
		if (clearOfCache()) {
			return dineSummarizeDao.deleteSummarize(summarizeId);	
		}
		return false;
	}
	
	private Object getElementFromCache(int key) {
		Element cacheElem = dineSummarizeCache.get(key);

		if (cacheElem != null) {
//			System.out.println("\n\n--------调试--[dineSummarize]--[从缓存中[取出]一个元素]-------\n\n");
			return cacheElem.getObjectValue();
		}
		return null;
	}
	
	private void doSaveElemToCache(int key, Object elem) {
		Element foodElement = dineSummarizeCache.get(key);
		if (foodElement == null) {
			dineSummarizeCache.put(new Element(key, elem));
//			System.out.println("\n\n--------调试--[dineSummarize]--[从缓存中[存入]一个元素]-------\n\n");
		}
	}

	// 清除指定key的缓存数据
	private boolean removeElementFromCache(int key) {
		if (dineSummarizeCache.get(key) != null) {
//			System.out.println("\n\n--------调试--[dineSummarize]--[从缓存中[移除]一个元素]-------\n\n");
			return dineSummarizeCache.remove(key);	
		}
		return true;
	}
	
	/**
	 * 清空缓存中所有数据
	 * */
	private boolean clearOfCache() {
		if (dineSummarizeCache.getSize() > 0) {
			dineSummarizeCache.removeAll();
//			System.out.println("\n\n--------调试--[dineSummarize]--[清空缓存数据]-------\n\n");
		}
		return true;
	}
}
